<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>渠道商管理</title>
    <script src="../commonCss.js"></script>
    <script src="../commonJs.js"></script>
    <link rel="stylesheet" href="../../css/common.css">
</head>

<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <div class="ctc_title">渠道商管理</div>
    <div class="down_line"></div>
</section>

<!-- Main content -->
<section class="content" >
    <div class="row">
        <div class="col-xs-12">
            <div class="box-header">
                <form class="form-inline">
                    <div class="form-group">
                        <label>查询：</label>
                        <input type="text"  class="form-control" id="searUser" placeholder="输入账号或渠道名称搜索">
                    </div>
                    <div class="form-group">
                        <button type="button"  onclick="getData()"  class="btn btn-block btn-primary glyphicon glyphicon-search">搜索</button>
                    </div>
                    <div style="float: right " id="right_but">
                        <div class="form-group" style="margin-right: 20px;">
                            <button type="button" onclick="addInfo()" class="btn btn-block btn-primary">新增</button>
                        </div>
                    </div>

                </form>
            </div>

            <div class="box-body" style="height:100%;">
                <table id="dataList" class="table table-bordered table-hover" >
                    <thead>
                    <tr>
                        <th>账号</th>
                        <th>渠道商名称</th>
                        <th>渠道商手机号</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody >
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>

<!-- 【编辑】弹出窗内容 -->
<div class="modal" id="editPanel" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width: 50%;height: 40%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" align="center" id="mopTitle"></h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" onsubmit="return false" id="form">
                    <fieldset  style="margin-top:2%;">
                        <input type="hidden" id="id" name="id">
                        <div class="form-group" id="mobi_div">
                            <label class="col-md-2 control-label red">账号</label>
                            <div class="col-md-10">
                                <input class="form-control" placeholder="必填项" type="text" id="account" onblur="checkAgentIsExit(this.value)">
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-md-2 control-label" >渠道商名称</label>
                            <div class="col-md-10">
                                <input class="form-control"  type="text" id="name"  placeholder="非必填项">
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-md-2 control-label" >渠道商电话</label>
                            <div class="col-md-10">
                                <input class="form-control"  type="text" id="mobile"  placeholder="非必填项">
                            </div>
                        </div>
                        <div class="form-group" id="pwd_div">
                            <label class="col-md-2 control-label">登录密码</label>
                            <div class="col-md-10">
                                <input  class="form-control" type="text"  id="password" placeholder="必填项">
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
            <div class="modal-footer" style="text-align: center">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="confirmEditConfig();">确定</button>
            </div>
        </div>
    </div>
</div>


<!-- page script -->
<script>

    $(document).ready(function() {
        //加载数据
        getData();
    });

    // 加载数据
    function getData(){
        var table = $('#dataList').DataTable({
            "destroy": true,   //可以重新初始化，用于搜索
            "lengthChange": false,
            "ordering": false,   //排序
            "info": true,		   //
            "autoWidth": false,   //宽度自动
            "paging": true,      //分页
            "searching": false,  //全局搜索关闭
            "processing": true,  //开启显示‘正在加载.../processing’
            "serverSide":true,    //true代表后台处理分页，false代表前台处理分页
            "bPaginate": false, //开关，是否显示分页器
            "bLengthChange": false, //开关，是否显示每页显示多少条数据的下拉框
            "ajax" :  {
                url:"/agent/getAgentList",
                async : false,
                type:"POST",
                data:{
                    key : $("#searUser").val(),
                }
            },
            //参数显示,"visible": false,
            "columns": [
                {"data": "account", 'sClass': "text-center",'width': '6%'},
                {"data": "name", 'sClass': "text-center",'width': '5%'},
                {"data": "mobile", 'sClass': "text-center",'width': '5%'},
                {"data": "createTime", 'sClass': "text-center",'width': '8%'},
                {"data": "id" , 'sClass': "text-center tx" ,'width':'3%',
                    "render": function (data, type, full, meta ) {
                        var str = "<a href = '#' title='修改' onclick=editData('" + data +"');><i class='\glyphicon glyphicon-pencil'></i></a>&nbsp;&nbsp;&nbsp;";
                            str += "<a href = '#' title='重置密码' onclick=restPwd('" + data +"');><i class='\glyphicon glyphicon-refresh'></i></a>&nbsp;&nbsp;&nbsp;";
                            str += "<a href = '#' title='删除' onclick=deleteData('" + data +"');><i class='\glyphicon glyphicon-trash'></i></a>&nbsp;&nbsp;&nbsp;";
                            return str;
                    }
                }
            ],

            //是否开启主题
            "bJQueryUI": true,
            "oLanguage": {    // 语言设置
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "sZeroRecords": "没有检索到数据",
                "sSearch": "检索:",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                }
            }
        });
    }

    //打开新增窗口
    function addInfo() {
        $("#mobi_div").show();
        $("#pwd_div").show();

        $("#id").val("");
        $("#account").val("");
        $("#mobile").val("");
        $("#name").val("");
        $("#password").val("");

        $("#mopTitle").text("新增渠道商");
        $("#editPanel").modal("toggle");//弹窗打开
    }

    //编辑渠道商
    function editData(id) {
        $("#mobi_div").hide();
        $("#pwd_div").hide();
        $.ajax({
            type : 'post',
            url : "/agent/getAgentById",
            async : true,//默认就是true
            dataType:"json",
            data:{
                id : id
            },
            success : function(res) {
                $("#id").val(res.id);
                $("#account").val(res.account);
                $("#mobile").val(res.mobile);
                $("#name").val(res.name);
                $("#password").val("");
            }
        });
        $("#mopTitle").text("编辑渠道商");
        $("#editPanel").modal("toggle");//弹窗打开
    }

    //校验账号是否存在
    function checkAgentIsExit(account) {
        $.ajax({
            type : 'post',
            url : "/agent/getAgentByAccount",
            async : true,//默认就是true
            dataType:"json",
            data:{
                account : account
            },
            success : function(data) {
                if(data.data != null){
                    layer.msg("该账号已存在！", {shift: -1, time: 500}, function(){
                        $("#account").val("");
                    });
                }
            }
        });
    }
    //保存
    function confirmEditConfig(){
        var id = $("#id").val();
        var account = $("#account").val();
        var mobile = $("#mobile").val();
        var password = $("#password").val();
        var name = $("#name").val();
        var url = "";
        //修改
        if(id != ""){
            url = "/agent/updateAgentInfo"
        }else{ //新增
            if(account == "" || password == ""){
                layer.msg("账号或密码不能为空！");
                return ;
            }
            url = "/agent/addAgent";
        }

        $.ajax({
            type : 'post',
            url : url,
            async : true,//默认就是true
            dataType:"json",
            data:{
                id : id,
                account : account,
                mobile : mobile,
                name : name,
                password : password
            },
            success : function(data) {
                $("#editPanel").modal("toggle");//弹窗打开
                layer.msg(data.msg, {shift: -1, time: 500}, function(){
                    location.href = "agentList.html";
                });
            }
        });
    }

    //删除
    function deleteData(id){
        if(confirm("确定要删除吗？")){
            $.ajax({
                type:"POST",
                url:"/agent/deleteAgent",
                async : true,//默认就是true
                dataType:"json",
                data:{ id : id},
                success : function(data) {
                    layer.msg(data.msg, {shift: -1, time: 500}, function(){
                        location.href = "agentList.html";
                    });

                }
            });
        }
    }

    //重置密码
    function restPwd(id) {
        if(confirm("密码将重置为：123456，确定要重置吗？")) {
            $.ajax({
                type: 'post',
                url: "/agent/restPwd",
                async: true,//默认就是true
                dataType: "json",
                data: {
                    id: id,
                    password: "123456"
                },
                success: function (data) {
                    layer.msg(data.msg, {shift: -1, time: 500}, function () {
                        location.href = "agentList.html";
                    });
                }
            });
        }
    }

</script>
</body>
</html>
